var __extends = (this && this.__extends) || function (d, b) {
    for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
    function __() { this.constructor = d; }
    d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
define(["require", "exports", "core/BaseWidget.class", "core/Functions.module"], function (require, exports, BaseWidget, Functions) {
    "use strict";
    var TerminalPanel = (function (_super) {
        __extends(TerminalPanel, _super);
        function TerminalPanel(settings, panelOpinions) {
            var _this = _super.call(this, settings) || this;
            _this.baseClass = "widget-panel";
            _this.isInnerWidgetLoaded = false;
            _this.panelHeightDelta = 78;
            _this.resizeMinHight = 80;
            _this.top = 56;
            _this.right = 50;
            _this.panelTitle = panelOpinions.panelTitle;
            _this.panelUniqClassName = panelOpinions.panelUniqClassName;
            _this.baseClass += '-' + _this.panelUniqClassName;
            _this.innerWidgetPath = panelOpinions.innerWidgetPath;
            _this.innerWidgetMain = panelOpinions.innerWidgetMain || '/Widget';
            return _this;
        }
        TerminalPanel.prototype.startup = function () {
            this.initContainer();
            this.setHtml(_.template(this.template)({
                height: this.mainContainerObj.height(),
                title: this.panelTitle,
                uniqClass: this.baseClass
            }), '#' + this.config.panelContainerID);
            this.configure();
            this.interactable();
            this.showLoading();
            this.initInnerWidget(this.innerWidgetPath, this.innerWidgetMain);
            this.focus(true);
        };
        TerminalPanel.prototype.configure = function () {
            this.loadingObj = this.domObj.find('.loading');
        };
        TerminalPanel.prototype.initContainer = function () {
            this.panelContainerWidget = $('#' + this.config.panelContainerID);
            if (this.panelContainerWidget.length > 0) {
            }
            else {
                this.panelContainerWidget = $('<div id="' + this.config.panelContainerID + '"></div>');
                $(this.AppX.appConfig.mainContainer).append(this.panelContainerWidget);
            }
            this.mainContainerObj = $(this.AppX.appConfig.mainContainer);
        };
        TerminalPanel.prototype.initInnerWidget = function (widgetPath, widgetMain) {
            require({}, [Functions.concatUrl(widgetPath, widgetMain)], function (Widget) {
                this.widgetObj = new Widget({
                    widgetPath: widgetPath,
                    panelSelector: '.' + this.baseClass + ' .panel-content',
                    readyCallback: this.onInnerWidgetReady.bind(this),
                    afterDestroyCallback: this.destroy.bind(this)
                });
            }.bind(this));
        };
        TerminalPanel.prototype.onInnerWidgetReady = function () {
            this.isInnerWidgetLoaded = true;
            this.hideLoading();
            this.domObj.find('.panel-content').show();
            setTimeout(this.rebuildHeight.bind(this), 1000);
            this.ready();
        };
        TerminalPanel.prototype.showLoading = function () {
            var _this = this;
            setTimeout(function () {
                if (!_this.isInnerWidgetLoaded) {
                    _this.loadingObj.show();
                }
            }, this.config.loadingDelay);
        };
        TerminalPanel.prototype.hideLoading = function () {
            this.loadingObj.remove();
        };
        TerminalPanel.prototype.rebuildHeight = function () {
            var domObjHeight = this.domObj.height(), domObjWidth = this.domObj.width(), mainContainerHeight = this.mainContainerObj.height();
            if (domObjHeight > mainContainerHeight - this.panelHeightDelta) {
                if (mainContainerHeight - this.panelHeightDelta > this.resizeMinHight) {
                    this.domObj.height(mainContainerHeight);
                }
            }
        };
        TerminalPanel.prototype.interactable = function () {
            var that = this;
            var windowObj = $(window);
            this.domObj.on('mousedown', '.panel-heading', function () {
                this.rebuildHeight();
            }.bind(this));
            this.domObj.draggable({
                handle: '.panel-heading',
                containment: this.AppX.appConfig.mainContainer,
                scroll: false,
                start: function () {
                    this.focus();
                }.bind(this)
            });
            this.domObj.delegate('.fold', 'click', function (event) {
                if (that.domObj.hasClass('folded') === true) {
                    that.domObj.removeClass('folded');
                    that.domObj.find('.fold span').removeClass('glyphicon-chevron-up').addClass('glyphicon-chevron-down');
                }
                else {
                    that.domObj.addClass("folded");
                    that.domObj.find('.fold span').removeClass('glyphicon-chevron-down').addClass('glyphicon-chevron-up');
                }
                this.rebuildHeight();
            }.bind(this));
            windowObj.on('resize.panel', function () {
                var domObjHeight = this.domObj.height(), domObjWidth = this.domObj.width(), mainContainerHeight = this.mainContainerObj.height();
                if (domObjHeight > mainContainerHeight - this.panelHeightDelta) {
                    if (mainContainerHeight - this.panelHeightDelta > this.resizeMinHight) {
                        that.domObj.height(mainContainerHeight);
                    }
                }
                if (that.domObj.offset().left + domObjWidth > windowObj.width()) {
                    that.domObj.css({
                        left: windowObj.width() - domObjWidth + "px"
                    });
                }
            }.bind(this));
            this.domObj.find('.close').on('click', function () {
                if (this.widgetObj) {
                    this.widgetObj.destroy.bind(this.widgetObj)();
                }
                else {
                    this.destroy();
                }
            }.bind(this));
            this.domObj.on('click', this.focus.bind(this));
            var dragging = false, docObj = $(document), tempTop = null;
            this.domObj.on('mousedown', '.panel-resize-y', function (e) {
                dragging = true;
                docObj.on('mousemove.panel', function (e) {
                    if (dragging) {
                        tempTop = this.domObj.offset().top;
                        if (tempTop + this.resizeMinHight < e.pageY) {
                            if (e.pageY - tempTop < this.mainContainerObj.height() - this.resizeMinHight) {
                                this.domObj.height(e.pageY - tempTop);
                            }
                        }
                    }
                }.bind(this));
                docObj.on('mouseup.panel', function (e) {
                    dragging = false;
                    docObj.off('mousemove.panel').off('mouseup.panel');
                }.bind(this));
            }.bind(this));
            this.domObj.on('click', 'fieldset>legend', function (e) {
                var target = $(e.currentTarget).next('.foldable');
                if (target.css('display') === "none") {
                    target.show(250);
                }
                else {
                    target.hide(250);
                }
                this.rebuildHeight();
            }.bind(this));
        };
        TerminalPanel.prototype.removeEvents = function () {
            $(window).off('resize.panel');
        };
        TerminalPanel.prototype.focus = function (autoFocus) {
            var len = this.panelContainerWidget.children().length;
            if (len > 1) {
                this.panelContainerWidget.addClass('focus');
                if (autoFocus === undefined || autoFocus !== true) {
                    if (this.domObj.index() < len - 1) {
                        this.panelContainerWidget.append(this.domObj);
                    }
                }
            }
            else {
                this.panelContainerWidget.removeClass('focus');
            }
        };
        TerminalPanel.prototype.destroy = function () {
            _super.prototype.destroy.call(this);
            this.removeEvents();
            this.domObj.remove();
            this.widgetObj = null;
            this.focus(true);
            this.afterDestroy();
        };
        return TerminalPanel;
    }(BaseWidget));
    return TerminalPanel;
});
